package com.xpgk.beetl.func;

import org.beetl.core.Context;
import org.beetl.core.Function;

import com.jfinal.log.Log;
import com.xpgk.constant.ConstantWebContext;
import com.xpgk.interceptor.AuthInterceptor;
import com.xpgk.mvc.right.Right;
import com.xpgk.mvc.syslog.Syslog;

/**
 * 页面按钮权限验证函数
 */
public class AuthUrl implements Function {

	private static final Log log = Log.getLog(AuthUrl.class);

	/**
	 * 页面按钮权限验证
	 */
	@Override
	public Object call(Object[] arg, Context context) {
		if (arg.length != 1 || null == arg[0]) {
			if (log.isErrorEnabled())
				log.error("权限标签验证，参数不正确");
			return false;
		}

		String url = null;
		String rightIds = null;
		String userIds = null;

		try {
			url = (String) arg[0];
			 Right right = Right.cacheGet(url);
			 if (null == right) {
				 log.error("URI不存在!");
			 }
			 rightIds = right.getPKValue();

			userIds = (String) context.getGlobal(ConstantWebContext.request_cUserIds);
		} catch (Exception e) {
			if (log.isErrorEnabled())
				log.error("权限标签验证，获取参数异常：" + e.getMessage());
			return false;
		}

		boolean bool = AuthInterceptor.hasPrivilegeUrl(rightIds, userIds);

		// if(log.isDebugEnabled()) log.debug("beetl HasPrivilegeUrl
		// 权限标签验证：userIds=" + userIds + "，url=" + url + "，验证结果" + bool);

		return bool;
	}

}
